import { Fragment } from 'react';
import { Button, Tooltip } from 'antd';

import { ColumnsRenderFun, DictionaryRenderFun, DictionaryRenderFuns, renderOverflowText } from '../common/Format';

import { EditOutlined, PlusOutlined, CloudUploadOutlined, CloseCircleOutlined, CloudDownloadOutlined } from '@ant-design/icons';
import { tooltipLength } from '@/utils';
import { SchemaManageAPI } from '@/components/SchemaComponent/SchemaManage/type';
import pageStyles from '@/common/pageLayout.less';
import { MaterialManageAPI } from '@/pages/PurchaseManage/MaterialManage/common/type';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';

/**
 * @getSorter
 * @description 排序
 * @param prev
 * @param next
 * @returns
 */
const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: MaterialManageAPI.IMaterialTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible: curRes?.['/expertInfoController/editExpert'] && record?.isOpen == 1,
      // && record?.createPositionID == getPosIDs() && record?.createrID == getUserID(),
    },
    {
      icon: <CloseCircleOutlined />,
      btnName: '作废',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible: curRes?.['/expertInfoController/cancelExpert'] && record?.isOpen == 1,
      // && record?.createPositionID == getPosIDs() && record?.createrID == getUserID(),
    },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * @getContractManageTableColumns
 * @description 合同管理数据表格信息展示和操作
 */

const getExpertsManageTableColumns = (operation: any, curRes): Record<string, any>[] => {
  const { onView } = operation;
  return [
    {
      title: '专家编号',
      dataIndex: 'id',
      key: 'id',
      visible: false,
      width: 150,
      fixed: 'left',
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '专家姓名',
      dataIndex: 'fullName',
      key: 'fullName',
      visible: true,
      width: 150,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.fullName, b?.fullName),
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              if (curRes?.['/expertInfoController/getExpertDetails/{id}']) {
                onView(record);
              }
            }}
            style={{ color: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '性别',
      dataIndex: 'sex',
      key: 'sex',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => getSorter(a?.sex, b?.sex),
      render: (text: string | null) => {
        let newText = '';
        if (text == '0') {
          newText = '未知';
        } else if (text == '1') {
          newText = '先生';
        } else if (text == '2') {
          newText = '女士';
        }
        return renderOverflowText(newText);
      },
    },
    {
      title: '所在单位',
      dataIndex: 'companyName',
      key: 'companyName',
      sorter: (a: any, b: any) => getSorter(a?.companyName, b?.companyName),
      visible: true,
      width: 150,
      render: (text: string) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '所在地',
      dataIndex: 'addressInfo',
      key: 'addressInfo',
      visible: true,
      width: 180,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '职务',
      dataIndex: 'positionName',
      key: 'positionName',
      visible: true,
      width: 150,
      render: (text: string) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '职称',
      dataIndex: 'rankTypeName',
      key: 'rankTypeName',
      visible: true,
      width: 150,
      render: (text: string) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '联系电话',
      dataIndex: 'phoneMobile',
      key: 'phoneMobile',
      visible: true,
      width: 130,
      sorter: (a: any, b: any) => getSorter(a?.phoneMobile, b?.phoneMobile),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '从事专业',
      dataIndex: 'industryTypeName',
      key: 'industryTypeName',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.industryTypeName, b?.industryTypeName),
      render: (text: string) => DictionaryRenderFuns({ text, isTooltip: true, wordNum: 100 }),
      width: 180,
    },
    {
      title: '研究方向',
      dataIndex: 'graduateMajor',
      key: 'graduateMajor',
      width: 150,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '毕业院校',
      dataIndex: 'graduateSchool',
      key: 'graduateSchool',
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '最高学历',
      dataIndex: 'educateLevelName',
      key: 'educateLevelName',
      visible: false,
      width: 150,
      render: (text: string) => DictionaryRenderFun({ text }),
    },

    {
      title: '人才称号',
      dataIndex: 'talentTypeName',
      key: 'talentTypeName',
      visible: false,
      width: 130,
      sorter: (a: any, b: any) => getSorter(a?.talentType, b?.talentType),
      render: (text: string) => DictionaryRenderFuns({ text, isTooltip: true, wordNum: 100 }),
    },
    {
      title: '海外经历',
      dataIndex: 'overseaTypeName',
      key: 'overseaTypeName',
      visible: false,
      width: 130,
      sorter: (a: any, b: any) => getSorter(a?.overseaType, b?.overseaType),
      render: (text: string) => DictionaryRenderFuns({ text, isTooltip: true, wordNum: 100 }),
    },
    // {
    //   title: '从事专业',
    //   dataIndex: 'industryTypeName',
    //   key: 'industryTypeName',
    //   width: 150,
    //   visible: false,
    //   render: (text: string) => DictionaryRenderFuns({ text, isTooltip: true, wordNum: 100 }),
    // },
    {
      title: '实验室项目评审经历',
      dataIndex: 'projectReview',
      key: 'projectReview',
      width: 250,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '证件类型',
      dataIndex: 'cardTypeName',
      key: 'cardTypeName',
      sorter: (a: any, b: any) => getSorter(a?.cardTypeName, b?.cardTypeName),
      width: 150,
      visible: false,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '证件号码',
      dataIndex: 'cardNum',
      key: 'cardNum',
      sorter: (a: any, b: any) => getSorter(a?.cardNum, b?.cardNum),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    // {
    //   title: '生日',
    //   dataIndex: 'birthday',
    //   key: 'birthday',
    //   visible: false,
    //   width: 150,
    //   render: (text: string) => renderOverflowText(moment(text).format('YYYY-MM-DD'), true),
    // },
    // {
    //   title: '简介',
    //   dataIndex: 'profileInfo',
    //   key: 'profileInfo',
    //   visible: false,
    //   width: 180,
    //   render: (text: string) => ColumnsRenderFun({ text }),
    // },
    {
      title: '邮箱',
      dataIndex: 'emailNum',
      key: 'emailNum',
      sorter: (a: any, b: any) => getSorter(a?.emailNum, b?.emailNum),
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },

    {
      title: '开户行名称',
      dataIndex: 'bankName',
      key: 'bankName',
      sorter: (a: any, b: any) => getSorter(a?.bankName, b?.bankName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '银行账户',
      dataIndex: 'bankNum',
      key: 'bankNum',
      sorter: (a: any, b: any) => getSorter(a?.bankNum, b?.bankNum),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },

    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: false,
      width: 180,
      sorter: (a: any, b: any) => getSorter(a?.createTime, b?.createTime),
      render: (text: string) => renderOverflowText(text, true),
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.createrName, b?.createrName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text }),
    },

    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 180,
      render: (text: string) => renderOverflowText(text, true),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyerName, b?.modifyerName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废类型',
      dataIndex: 'cancelType',
      key: 'cancelType',
      visible: false,
      width: 120,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 150,
      render: (text: string) => renderOverflowText(text, true),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 150,
      render: (text: string) => renderOverflowText(text, true),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人公司',
      dataIndex: 'cancelCompanyName',
      key: 'cancelCompanyName',
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '版本号',
      dataIndex: 'versionNum',
      key: 'versionNum',
      visible: false,
      width: 100,
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 180,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (_: string, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operation, record)}</div>;
      },
    },
  ];
};

/**
 * @getExpertsManageTableHeaderActions
 * @description 专家库管理表格顶部自定义按钮
 * @returns SchemaManageAPI.IActionBtnList[]
 */
const getExpertsManageTableHeaderActions = (operation: any, curRes): SchemaManageAPI.IActionBtnList[] => {
  const { onAdd, onBatchImport, onExport } = operation;
  return [
    {
      title: '添加',
      visible: curRes?.['/expertInfoController/addExpert'],
      type: 'primary',
      icon: <PlusOutlined />,
      onClick: onAdd,
    },
    {
      title: '导入',
      visible: curRes?.['/expertInfoController/import'] && curRes?.['/expertInfoController/batchImportExpert'] ? true : false,
      type: 'primary',
      icon: <CloudUploadOutlined />,
      onClick: onBatchImport,
    },
    {
      // title: <Tooltip title="可导出当前筛选出的专家数据">导出</Tooltip>,
      title: '导出',
      visible: curRes?.['/expertInfoController/export'],
      type: 'primary',
      icon: <CloudDownloadOutlined />,
      onClick: onExport,
    },
  ];
};

export { getExpertsManageTableColumns, getExpertsManageTableHeaderActions };
// {
//   title: '所在地',
//   dataIndex: 'provinceCode',
//   key: 'provinceCode',
//   width: 180,
//   visible: true,
//   render: (_: string | null, record: Record<string, any>) => {
//     const newText = renderRegionText(record);
//     return (
//       <Tooltip title={tooltipLength(newText, 100)}>
//         <span className={pageStyles['over-flow-hidden']}>{newText ? newText : '--'}</span>
//       </Tooltip>
//     );
//   },
// },
// {
//   title: '备用手机',
//   dataIndex: 'phoneSpare',
//   key: 'phoneSpare',
//   width: 130,
//   visible: false,
//   sorter: (a: any, b: any) => getSorter(a?.phoneSpare, b?.phoneSpare),
//   render: (text: string) => ColumnsRenderFun({ text }),
// },
// {
//   title: '办公电话',
//   dataIndex: 'phoneOffice',
//   key: 'phoneOffice',
//   visible: false,
//   width: 130,
//   sorter: (a: any, b: any) => getSorter(a?.phoneOffice, b?.phoneOffice),
//   render: (text: string) => ColumnsRenderFun({ text }),
// },
// {
//   title: '所在部门',
//   dataIndex: 'deptName',
//   sorter: (a: any, b: any) => getSorter(a?.deptName, b?.deptName),
//   key: 'deptName',
//   visible: false,
//   width: 150,
//   render: (text: string) => DictionaryRenderFun({ text }),
// },
// {
//   title: '是否有效',
//   dataIndex: 'isOpen',
//   key: 'isOpen',
//   width: 120,
//   visible: true,
//   sorter: (a: any, b: any) => (a?.isOpen > b?.isOpen ? 1 : -1),
//   render: (text: string | null, record: Record<string, any>) => {
//     return (
//       <Tooltip title={tooltipLength(text, 100)}>
//         <span className={pageStyles['over-flow-hidden']}>{Number(text) == 0 ? '否' : '是'}</span>
//       </Tooltip>
//     );
//   },
// },

// {
//   title: '专家类型',
//   dataIndex: 'expertTypeName',
//   key: 'expertTypeName',
//   visible: true,
//   sorter: (a: any, b: any) => getSorter(a?.expertTypeName, b?.expertTypeName),
//   render: (text: string) => DictionaryRenderFun({ text }),
//   width: 150,
// },
// {
//   title: '专家级别',
//   dataIndex: 'expertLevelName',
//   key: 'expertLevelName',
//   visible: true,
//   sorter: (a: any, b: any) => getSorter(a?.expertLevelName, b?.expertLevelName),
//   render: (text: string) => DictionaryRenderFun({ text }),
//   width: 150,
// },
// {
//   title: '是否在岗',
//   dataIndex: 'isDuty',
//   key: 'isDuty',
//   visible: true,
//   sorter: (a: any, b: any) => getSorter(a?.isDuty, b?.isDuty),
//   width: 120,
//   render: (text: string) => {
//     if (!text) return <span>--</span>;
//     if (text == '1') {
//       return <span>是</span>;
//     } else if (text == '0') {
//       return <span>否</span>;
//     } else {
//       return <span>--</span>;
//     }
//   },
// },
// {
//     title: '是否启用',
//     dataIndex: 'isOpen',
//     key: 'isOpen',
//     sorter: (a: any, b: any) => getSorter(a?.isOpen, b?.isOpen),
//     visible: true,
//     width: 120,
//     render: (text: string) => renderOverflowText(text),
// },
// {
//   title: '政治面貌',
//   dataIndex: 'politicalTypeName',
//   key: 'politicalTypeName',
//   visible: false,
//   sorter: (a: any, b: any) => getSorter(a?.politicalTypeName, b?.politicalTypeName),
//   render: (text: string) => DictionaryRenderFun({ text }),
//   width: 150,
// },
// {
//   title: '民族',
//   dataIndex: 'nationTypeName',
//   key: 'nationTypeName',
//   sorter: (a: any, b: any) => getSorter(a?.nationTypeName, b?.nationTypeName),
//   width: 120,
//   visible: false,
//   render: (text: string) => DictionaryRenderFun({ text }),
// },
// {
//   title: '归属时间',
//   dataIndex: 'ownTime',
//   key: 'ownTime',
//   visible: false,
//   width: 150,
//   render: (text: string) => renderOverflowText(text, true),
// },
// {
//   title: '归属人',
//   dataIndex: 'ownerName',
//   key: 'ownerName',
//   visible: false,
//   width: 150,
//   render: (text: string) => ColumnsRenderFun({ text }),
// },
// {
//   title: '归属岗位',
//   dataIndex: 'ownPositionName',
//   key: 'ownPositionName',
//   visible: false,
//   width: 150,
//   render: (text: string) => renderOverflowText(text, true),
// },
// {
//   title: '归属部门',
//   dataIndex: 'ownDepartmentName',
//   key: 'ownDepartmentName',
//   visible: false,
//   width: 150,
//   render: (text: string) => renderOverflowText(text, true),
// },
// {
//   title: '归属人公司',
//   dataIndex: 'ownCompanyName',
//   key: 'ownCompanyName',
//   visible: false,
//   width: 150,
//   render: (text: string) => renderOverflowText(text, true),
// },
// {
//     title: '证书类型',
//     dataIndex: 'attachTypeName',
//     key: 'attachTypeName',
//     visible: true,
//     width: 120,
//     render: (text: string) => renderOverflowText(text, true),
// },
// {
//     title: '证件描述',
//     dataIndex: 'attachContent',
//     key: 'attachContent',
//     visible: true,
//     width: 180,
//     render: (text: string) => renderOverflowText(text, true),
// },
// {
//     title: '证件地址',
//     dataIndex: 'attachURL',
//     key: 'attachURL',
//     visible: true,
//     width: 180,
//     render: (text: string) => renderOverflowText(text, true),
// },
// {
//     title: '是否永久有效',
//     dataIndex: 'isExpire',
//     key: 'isExpire',
//     visible: true,
//     width: 180,
//     render: (text: string) => renderOverflowText(text),
// },
